﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.Entities.Commodities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.Tags;
    using Newtonsoft.Json;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Linq;

    [ParseChildren(true)]
    public class AliOHProductDetails : AliOHTemplatedWebControl
    {
        private Common_ExpandAttributes expandAttr;
        private HtmlInputHidden hidden_skus;
        private HyperLink linkDescription;
        private Literal litConsultationsCount;
        private Literal litDescription;
        private HtmlInputHidden litHasCollected;
        private Literal litMarketPrice;
        private Literal litProdcutName;
        private Literal litReviewsCount;
        private Literal litSalePrice;
        private Literal litShortDescription;
        private Literal litSoldCount;
        private Literal litStock;
        private int productId;
        private AliOHTemplatedRepeater rptProductImages;
        private Common_SKUSelector skuSelector;

        protected override void AttachChildControls()
        {
            if (!int.TryParse(this.Page.Request.QueryString["productId"], out this.productId))
            {
                base.GotoResourceNotFound("");
            }
            this.rptProductImages = (AliOHTemplatedRepeater) this.FindControl("rptProductImages");
            this.litProdcutName = (Literal) this.FindControl("litProdcutName");
            this.litSalePrice = (Literal) this.FindControl("litSalePrice");
            this.litMarketPrice = (Literal) this.FindControl("litMarketPrice");
            this.litShortDescription = (Literal) this.FindControl("litShortDescription");
            this.litDescription = (Literal) this.FindControl("litDescription");
            this.litStock = (Literal) this.FindControl("litStock");
            this.skuSelector = (Common_SKUSelector) this.FindControl("skuSelector");
            this.linkDescription = (HyperLink) this.FindControl("linkDescription");
            this.expandAttr = (Common_ExpandAttributes) this.FindControl("ExpandAttributes");
            this.litSoldCount = (Literal) this.FindControl("litSoldCount");
            this.litConsultationsCount = (Literal) this.FindControl("litConsultationsCount");
            this.litReviewsCount = (Literal) this.FindControl("litReviewsCount");
            this.litHasCollected = (HtmlInputHidden) this.FindControl("litHasCollected");
            this.hidden_skus = (HtmlInputHidden) this.FindControl("hidden_skus");
            int? maxReviewNum = null;
            ProductBrowseInfo info = ProductBrowser.GetProductBrowseInfo(this.productId, maxReviewNum, null);
            IEnumerable enumerable = from item in info.Product.Skus select item.Value;
            this.hidden_skus.Value = JsonConvert.SerializeObject(enumerable);
            if (info == null)
            {
                base.GotoResourceNotFound("此商品已不存在");
            }
            if (info.Product.SaleStatus != ProductSaleStatus.OnSale)
            {
                base.GotoResourceNotFound("此商品已下架");
            }
            if (this.rptProductImages != null)
            {
                string locationUrl = "javascript:;";
                SlideImage[] imageArray = new SlideImage[] { new SlideImage(info.Product.ImageUrl1, locationUrl), new SlideImage(info.Product.ImageUrl2, locationUrl), new SlideImage(info.Product.ImageUrl3, locationUrl), new SlideImage(info.Product.ImageUrl4, locationUrl), new SlideImage(info.Product.ImageUrl5, locationUrl) };
                this.rptProductImages.DataSource = from item in imageArray
                    where !string.IsNullOrWhiteSpace(item.ImageUrl)
                    select item;
                this.rptProductImages.DataBind();
            }
            this.litProdcutName.Text = info.Product.ProductName;
            this.litSalePrice.Text = info.Product.MinSalePrice.ToString("F0");
            if (info.Product.MarketPrice.HasValue)
            {
                this.litMarketPrice.SetWhenIsNotNull(info.Product.MarketPrice.GetValueOrDefault(0M).ToString("F0"));
            }
            this.litShortDescription.Text = info.Product.ShortDescription;
            if ((this.litDescription != null) && !string.IsNullOrWhiteSpace(info.Product.Description))
            {
                Regex regex = new Regex("<script[^>]*?>.*?</script>", RegexOptions.IgnoreCase);
                if (!string.IsNullOrEmpty(info.Product.MobblieDescription))
                {
                    this.litDescription.Text = regex.Replace(info.Product.MobblieDescription, "");
                }
                else
                {
                    this.litDescription.Text = regex.Replace(info.Product.Description, "");
                }
            }
            this.litSoldCount.SetWhenIsNotNull(info.Product.ShowSaleCounts.ToString());
            this.litStock.Text = info.Product.Stock.ToString();
            this.skuSelector.ProductId = this.productId;
            if (this.expandAttr != null)
            {
                this.expandAttr.ProductId = this.productId;
            }
            if (this.linkDescription != null)
            {
                this.linkDescription.NavigateUrl = "/AliOH/ProductDescription.aspx?productId=" + this.productId;
            }
            this.litConsultationsCount.SetWhenIsNotNull(info.ConsultationCount.ToString());
            this.litReviewsCount.SetWhenIsNotNull(info.ReviewCount.ToString());
            Member user = HiContext.Current.User as Member;
            bool flag = false;
            if (user != null)
            {
                flag = ProductBrowser.CheckHasCollect(user.UserId, this.productId);
            }
            this.litHasCollected.SetWhenIsNotNull(flag ? "1" : "0");
            PageTitle.AddSiteNameTitle("商品详情");
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-VProductDetails.html";
            }
            base.OnInit(e);
        }
    }
}

